# -*-coding:utf-8
# ProjectName: smalleel-api-tools
# FileName: smalleelLogin.py
# @Time :2022/9/26 10:39
# @Author :
# @Description:小程序,物业，平台登录,获取token

from common.util.jsonUtil.jsonUtil import config, write_json, read_sql_json
from common.util.md5Util.stringTomd5 import login_md5
from common.util.mySqlUtil.mySqlUtil import select_sql
from common.util.requestUtil.requestUtil import RequestsUtil
from tool.process.config.smalleelApi import *


class Login:
    def __init__(self):
        self.req = RequestsUtil()
        self.username = config.get_user_info("usernamePro")
        self.password = config.get_user_info("passwordPro")

    # 小程序登录
    def login_merchant(self):
        """
        物业小程序登录接口
        :return:小程序的token
        """
        url = MerchantApi.merchantLogin
        params = {"username": self.username, "password": login_md5(self.username, self.password)}
        response = self.req.send_request("post", url, params, "json")
        if response["code"] == 200:
            writeJsonData = {"tokenMerchant": response["data"]["token"],
                             "proid": response["data"]["thisProId"],
                             "optPwd": select_sql(read_sql_json('merchant_sql', 'selectOpt', 1, self.username))[0][0],
                             "username": response["data"]["username"]}
            write_json(writeJsonData, 2)
        return response

    # 云平台登录
    def login_platform(self):
        """
        云平台登录接口
        """
        url = PlatformApi.platformlogin
        params = {"username": self.username, "password": login_md5(self.username, self.password)}
        response = self.req.send_request("post", url=url, params=params, parameterFormat="data")
        if response["code"] == 200:
            writeJsonData = {"tokenPlatform": response["data"]["userInfo"]["token"]}
            write_json(writeJsonData, 6)
        else:
            print(response)

    # 物业web登录
    def login_smalleel(self):
        """
        电表后台登录接口
        """
        url = SmalleelApi.smalleellogin
        params = {"username": self.username, "password": login_md5(self.username, self.password)}
        response = self.req.send_request("post", url=url, params=params, parameterFormat="data")
        userInfoPro = dict(response["data"]["userInfo"])
        if response["code"] == 200:
            writeJsonData = {"tokenPro": userInfoPro["token"],
                             "optPwdPro": userInfoPro["optPwd"],
                             "proid": userInfoPro["lastproid"],
                             "usernamePro": params["username"]
                             }
            write_json(writeJsonData)
        else:
            print(response)

    def login_smalleel_return(self) -> dict:
        """
        电表后台登录接口
        """
        url = SmalleelApi.smalleellogin
        params = {"username": self.username, "password": login_md5(self.username, self.password)}
        response = self.req.send_request("post", url=url, params=params, parameterFormat="data")
        userInfoPro = dict(response["data"]["userInfo"])
        return userInfoPro["token"]
        # return response

    def login_merchant_return(self):
        """
        物业小程序登录接口
        :return:小程序的token
        """
        url = MerchantApi.merchantLogin
        params = {"username": self.username, "password": login_md5(self.username, self.password)}
        response = self.req.send_request("post", url, params, "json")
        return response["data"]["token"]


if __name__ == '__main__':
    # print(Login().login_smalleel())
    # print(Login().login_merchant())
    print(Login().login_merchant_return())
